home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6991 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.7 KB  |  117 lines

  1. Path: newshost.cyberramp.net!news
  2. From: sinan@cyberramp.net (John Noland)
  3. Newsgroups: comp.os.msdos.programmer,comp.lang.c
  4. Subject: Re: open vs fopen?
  5. Date: 17 Feb 1996 01:01:24 GMT
  6. Organization: Prose Software
  7. Message-ID: <4g39d4$ej8@newshost.cyberramp.net>
  8. References: <uEYFxc9nX8WX083yn@mbnet.mb.ca> <4f8bev$6tr@hermes.louisville.edu> <2d3avbl60.alamito@marketgraph.xs4all.nl> <4ftusv$181@newshost.cyberramp.net> <danpop.824430285@rscernix>
  9. NNTP-Posting-Host: ramp3-28.cyberramp.net
  10. X-Newsreader: WinVN 0.99.5
  11.  
  12. In article <danpop.824430285@rscernix>, danpop@mail.cern.ch says...
  13. >
  14. >In <4ftusv$181@newshost.cyberramp.net> sinan@cyberramp.net (John Noland) writes:
  15. >
  16. >>The open() function and its ilk are normally referred to as the "low-level"
  17. >>I/O package. fopen() is the "Buffered" or "Standard" I/O package. The 
  18. >>strength of the low-level I/O functions is that they offer excellent control,
  19. >>particularly when used with binary files.
  20. >
  21. >???  What can read/write do on a binary file that fread/fwrite cannot do?
  22.  
  23. Where in the above did I compare them with each other? 
  24.  
  25. >
  26. >>If you have a special I/O need, you
  27. >>can use the low-level I/O routines to fashion the exact I/O package to fit
  28. >>your needs. 
  29. >
  30. >Same question as above.
  31.  
  32. Same question from me again. Are the read/write functions written using fread/
  33. fwrite or is it vice versa? I believe, and feel free to correct me if I'm 
  34. wrong, that the fread/fwrite functions are written using the read/write 
  35. functions.
  36.  
  37. >
  38. >>The standard I/O package is one such creation. It is designed to provide fast
  39. >>buffered I/O, mostly for text situations.
  40. >               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  41. >???
  42.  
  43. I use the stdio routines almost exclusively myself. Even for binary files. 
  44. That doesn't make the above statement wrong. Your statements seem to imply 
  45. that all systems are buffered at the OS level, so who cares what I/O 
  46. approach you use. You're not going to see any difference in I/O speed no 
  47. matter what. While this may be true for your particular situation, it's by 
  48. no means universal. The usefulness of buffering is in working sequentially
  49. with a file. This makes buffered I/O well suited for text files. That 
  50. doesn't mean you can't or won't read a binary file sequentially or that it 
  51. sucks for use with binary files. Which, judging from what you've written,
  52. is exactly what you'll think I meant. 
  53.  
  54. >>For a lot of applications, using the
  55. >>standard I/O package is simpler and more effective than using simple low-level
  56. >>output. The essential feature is its use of automatic buffering. Buffered 
  57. >>I/O means reading and writing data in large chunks from a file to an array
  58. >>and back. Reading and writing data in large chunks greatly speeds up the I/O
  59. >>operations, while storing in an array allows access to the individual bytes.
  60. >>The advantages of this approach should be apparent.
  61. >
  62. >Except that you can read and write data in large chunks using stdio 
  63. >routines.  The impact of the extra level of buffering is insignificant.
  64.  
  65. Isn't the above blurb talking about the stdio routines? Once again, your 
  66. reference to an extra level of buffering is not applicable to all systems. 
  67. If you're really referring to the low-level routines, then your statement 
  68. is not always true.
  69.  
  70. >
  71. >>When fopen() is used, several things happen. The file, of course, is opened.
  72. >>Second, an external character array is created to act as a buffer. The 
  73. >><stdio.h> file has this buffer set to a size of 512 bytes.
  74. >
  75. >To a default size of _minimum_ 256 bytes.  I'm typing this text on a
  76. >system which uses a default buffer size of 8192 bytes.  The size of the
  77. >buffer can be controlled by the programmer via the setvbuf function.
  78. >
  79.  
  80. That's a big default buffer. How many disk accesses does it take to fill 
  81. that thing? I seriously doubt you would want a buffer that big when reading 
  82. a file randomly. But, maybe your system is so fast you can afford that kind 
  83. of waste. In my <stdio.h> file, the constant BUFSIZ is 512. You say that 
  84. you can control the size of the buffer using setvbuf(). That's true, but it
  85. doesn't change the default buffer size as you seem to imply. If I do the 
  86. following:
  87.  
  88. FILE *input, *output;
  89.  
  90. input = fopen("myfile.in", "r+b");
  91. setvbuf(input, NULL, _IOFBF, 1024);
  92.  
  93. output = fopen("myfile.out", "w+b");
  94.  
  95. do you think output points to a buffer of the size set by setvbuf?  I don't 
  96. think this is what you meant. 
  97.  
  98.  
  99. >>Technically, in DOS, all I/O is buffered since DOS itself uses buffers
  100. >>for disk I/O. 
  101. >
  102. >The same applies to other operating systems (e.g. Unix) except that they
  103. >do a much better job at buffering I/O than MSDOS.
  104.  
  105. Some versions of UNIX don't do any buffering at all.
  106.  
  107. >
  108. >>Hope this clarifies things.
  109. >
  110. >So many inaccurate statements don't clarify things.  On the contrary.
  111.  
  112. The shit you have to up with when trying to be helpful. 
  113.  
  114. John
  115.  
  116.  
  117.